<?php
class Model_Post extends Model{

    public function getListInfo($offset,$pageSize,$assort,$sort,$time){

        $whereSql="";

        $orderSql="";

        switch ($sort) {

            case 'view':

                $orderSql='order by t.view desc,t.created desc';

                break;

            case 'wish':

                $orderSql='order by t.wish desc,t.created desc';
                
                break;

            case 'review':

                $orderSql='order by t.review desc,t.created desc';
                
                break;

            case 'review':

                $orderSql='order by t.review desc,t.created desc';
                
                break;

            default:

                $orderSql='order by t.created desc'; 

                break;
        }

        switch ($time) {

            case 'week':

                $orderSql='order by t.review desc,t.created desc';

                $whereSql='and t.created>DATE_SUB(CURDATE(), INTERVAL 1 WEEK)';
                
                break;

            case 'month':

                $orderSql='order by t.review desc,t.created desc';

                $whereSql='and t.created>DATE_SUB(CURDATE(), INTERVAL 1 MONTH)';
                
                break;

            case '3month':

                $orderSql='order by t.review desc,t.created desc';

                $whereSql='and t.created>DATE_SUB(CURDATE(), INTERVAL 3 MONTH)';
                
                break;

            case '6month':

                $orderSql='order by t.review desc,t.created desc';

                $whereSql='and t.created>DATE_SUB(CURDATE(), INTERVAL 6 MONTH)';
                
                break;

            case 'year':

                $orderSql='order by t.review desc,t.created desc';

                $whereSql='and t.created>DATE_SUB(CURDATE(), INTERVAL 1 YEAR)';
                
                break;

            default:

                $orderSql='order by t.created desc'; 

                break;
        }

        if(!empty($assort)){

            $whereSql=" and t.assort='$assort' ";
        }

        $sql="select t.postid id,t.userid,t.title,t.description,t.tags,t.images,t.url,t.price,t.isdisabled,t.view,t.reach,t.review,t.created,t.wish,t.praiseup,t.praisedown
             from post t
             where t.isdeleted<>'1' ".$whereSql.
             " and t.type='1' "
             ." ".$orderSql.
             " limit $offset,$pageSize";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return $data;

    }

     public function getListItems($assort,$time){

        $whereSql="";

        switch ($time) {

            case 'week':

                $whereSql='and t.created>DATE_SUB(CURDATE(), INTERVAL 1 WEEK)';
                
                break;

            case 'month':

                $whereSql='and t.created>DATE_SUB(CURDATE(), INTERVAL 1 MONTH)';
                
                break;

            case '3month':

                $whereSql='and t.created>DATE_SUB(CURDATE(), INTERVAL 3 MONTH)';
                
                break;

            case '6month':

                $whereSql='and t.created>DATE_SUB(CURDATE(), INTERVAL 6 MONTH)';
                
                break;

            case 'year':

                $whereSql='and t.created>DATE_SUB(CURDATE(), INTERVAL 1 YEAR)';
                
                break;

            default:

                break;
        }

        if(!empty($assort)){

            $whereSql=" and t.assort='$assort' ";
        }

        $totalsql="select count(1)total
             from post t
             where t.isdeleted<>'1' ".$whereSql;

        $total=DB::query(Database::SELECT,$totalsql)->execute()->get("total");

        return $total;

    }

    public  function saveInfo($id,$userid,$album,$title,$description,$url,$tags,$price,$isdisabled){

        if($id==""){

            return $this->add($userid,$album,$title,$description,$url,$tags,$price,$isdisabled);

        }
        else{

            return $this->alter($id,$userid,$album,$title,$description,$url,$tags,$price,$isdisabled);
        }

    }
    private function add($userid,$album,$title,$description,$url,$tags,$price,$isdisabled){

        try {


            list($insert_id, $total_rows)= DB::insert('post')

            ->columns(array('userid','album','title','description','url','tags','price','isdisabled','created'))
                
            ->values(array($userid,$album,$title,$description,$url,$tags,$price,$isdisabled,date("Y-m-d h:i:sa")))->execute();

            Database::instance()->commit();

            return json_encode(array("info"=>"success","count"=>$total_rows,"id"=>$insert_id));
        }
        catch (Exception $e) {

            Database::instance()->rollback();

            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
    private function alter($id,$userid,$album,$title,$description,$url,$tags,$price,$isdisabled){

        try {
            $total_rows=DB::update('post')

            ->set(array('userid'=>$userid,'album'=>$album,'title'=>$title,
                'description'=>$description,'url'=>$url,
                'tags'=>$tags,'price'=>$price,'isdisabled'=>$isdisabled))

            ->where('postid','=',$id)->execute();

            Database::instance()->commit();

            return json_encode(array("info"=>"success","count"=>$total_rows));
        }
        catch (Exception $e) {

            Database::instance()->rollback();

            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
    public function getUserPost($userid,$id){

            $sql="select t.postid id,t.userid,t.album albumid,t.introduction,
                a.caption album,t.title,t.description,t.tags,t.images,t.url,t.price,t.assort
                from post t
                left join site_user s on s.site_userid=t.userid
                left join album a on a.albumid=t.album
                where t.isdeleted<>'1'
                and s.site_userid='$userid'
                and t.postid='$id'
                limit 1";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        if(!empty($data)){

            return $data[0];
        }

        return $data;

    }
    public function saveAlbumInfo($id,$userid,$caption,$special,$isdisabled){

        if($id==""){

            try {

                list($insert_id, $total_rows)= DB::insert('album')

                ->columns(array('caption','special','isdisabled','userid','createdon'))
                    
                ->values(array($caption,$special,$isdisabled,$userid,date("Y-m-d h:i:sa")))->execute();

                Database::instance()->commit();

                return json_encode(array("info"=>"success","count"=>$total_rows,"id"=>$insert_id));
            }
            catch (Exception $e) {

                Database::instance()->rollback();

                return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
            }

        }
        else{

            try {
                $total_rows=DB::update('album')

                ->set(array('caption'=>$caption,'special'=>$special,'isdisabled'=>$isdisabled,'userid'=>$userid))

                ->where('albumid','=',$id)->execute();

                Database::instance()->commit();

                return json_encode(array("info"=>"success","count"=>$total_rows));
            }
            catch (Exception $e) {

                Database::instance()->rollback();

                return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
            }
        }
    }
    public function uploadImage($id,$filepath,$filename){
        try {

            $upFilePath ='temp/post/post'.$id.'-M.jpg';

            $model=new Model_Admin_Common();

            $result=$model->upload_file($filepath,$upFilePath,'');

            if($result){

                 $total_rows=DB::update('post')->set(array('image'=>$upFilePath))

                ->where('postid','=',$id)->execute();

                 Database::instance()->commit();

                return true;
            }
        }
        catch (Exception $e) {
            Database::instance()->rollback();
            return false;
        }
    }
    public function getPostById($id){

        $sql="select t.postid id,t.userid,t.title,t.description,t.tags,t.images,t.url,t.price,s.fullname,s.photo,t.created,t.wish,t.praiseup,t.praisedown
                from post t
                left join site_user s on s.site_userid=t.userid
                where t.postid='$id'
                limit 1";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        if(!empty($data)){

            return $data[0];
        }

        return $data;

    }

    public function getPostByUser($userid,$offset,$pageSize){

        $sql="select count(1)total
             from post t
             where t.isdeleted<>'1'
             and t.userid='$userid'";

        $total=DB::query(Database::SELECT,$sql)->execute()->get("total");

        $sql="select t.postid id,t.title,t.description,t.images,t.view,t.reach,t.review,t.created
             from post t
             left join album a on a.albumid=t.album
             where t.isdeleted<>'1'
             and t.userid='$userid'
             order by t.created desc limit $offset,$pageSize";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return array("total"=>$total,"rows"=>$data);

    }

    public function getPraiseByUser($userid,$offset,$pageSize){

        $sql="select p.postid id,p.title,p.description,p.images,p.view,p.review,p.created,p.wish,p.praiseup,p.praisedown
            from
            (
                select i.postid
                from interact i
                where  i.userid='$userid'
                group by i.postid
                order by i.createdon desc limit $offset,$pageSize
            )t
            left join post p on p.postid=t.postid
            ";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return $data;

    }

    public function getPraiseItemsByUser($userid){

        $total= DB::select(array(DB::expr('count(1)'), 'total'))

         ->from('interact')

        ->where('type','=','praiseup')

        ->and_where("userid","=",$userid)

        ->execute()

        ->get("total");

        return $total;

    }

    public function getReviewByUser($userid,$offset,$pageSize){

        $sql="select p.postid id,p.title,p.description,p.images,p.view,p.review,p.created,p.wish,p.praiseup,p.praisedown
            from
            (
                select postid
                from review r
                where  r.userid='$userid'
                group by r.postid
                order by r.createdon desc limit $offset,$pageSize
            )t
            left join post p on p.postid=t.postid
            ";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return $data;

    }

    public function getReviewItemsByUser($userid){

        $total= DB::select(array(DB::expr('count(1)'), 'total'))

         ->from('review')

        ->where('userid','=',$userid)

        ->execute()

        ->get("total");

        return $total;

    }

    public function remove($userid,$id){

      DB::update('post')->set(array('isdeleted'=>'1'))->where('postid','=',$id)->and_where("userid","=",$userid)->execute();

      return array("code"=>"0x00000");

   }
}